﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private float dis;
    private float time;
    public  float atk;
    public  CharacterController CharCollider;
    public  CapsuleCollider CapsuleCollider;

    void Start()
    {
        //Physics.IgnoreCollision(GetComponent<Collider>(), GameObject.Find("Character").GetComponent<Collider>(), true);
        // GameObject temp = this.gameObject.transform.parent.gameObject;
        if (CapsuleCollider != null)
        {  
            Physics.IgnoreCollision(CapsuleCollider, this.GetComponent<SphereCollider >(), true);
        }
        else if(CharCollider!=null)
        {
            Physics.IgnoreCollision(CharCollider, this.GetComponent<SphereCollider>(), true);
        }
    }

    void Update()
    {
        transform.Translate(transform.forward * Time.deltaTime * Player.bulletSpeed, Space.World);
        time += Time.deltaTime;
        dis = Player.bulletSpeed * time;

        if (dis >= Player.Insatance.atkRange)//当子弹超出最大攻击距离，自动销毁
        {
            Destroy(gameObject);
        }
    }

    public void OnTriggerEnter(Collider col)
    {
          
        if (col.gameObject.tag == "Enemy")
        {
            col.GetComponent<Enemy>().GetAttack(atk); //攻击指令
            bullletDestory();
        }
        else if(col.gameObject.tag == "Player")
        {
            col.GetComponent<Player>().GetAttack(atk);
            bullletDestory();
        }else if (col.gameObject.tag == "Wall")
        {
            bullletDestory();
        }
    }

    /// <summary> 销毁子弹 </summary>
    public void bullletDestory()
    {
        //GameObject bulletEffect = Instantiate(爆炸特效, transform.position, transform.rotation);
        Destroy(gameObject);
        //Destroy(bulletEffect, 0.8f);
    }
}
